package com.zg.domain;

import io.netty.channel.Channel;

import java.util.LinkedList;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 使用单例模式来充当数据库
 */
public class DataForClient {

    //在线设备
    private static ConcurrentHashMap<Channel,String> onlineMap;

    //在线设备详细信息
    private static ConcurrentHashMap<String, LotClient> clientDetail;

    //这里并发量较大的时候要考虑线程安全问题，将list换成CopyOnWriteArrayList
    private static ConcurrentHashMap<String, LinkedList<Channel>> topic_client;

    private static ConcurrentHashMap<String,String> user_password;

    private DataForClient(){

    }

    private volatile static DataForClient dataForClient;

    public static DataForClient getDataForClient(){
        if (dataForClient==null){
            synchronized (DataForClient.class){
                if (dataForClient==null){
                    onlineMap = new ConcurrentHashMap<>();
                    clientDetail = new ConcurrentHashMap<>();
                    topic_client = new ConcurrentHashMap<>();
                    user_password = new ConcurrentHashMap<>();
                    dataForClient = new DataForClient();
                }
            }
        }
        return dataForClient;
    }


    public static ConcurrentHashMap getOnlineMap(){
        if (onlineMap==null){
            getDataForClient();
        }
        return onlineMap;
    }


    public static ConcurrentHashMap getClientDetail(){
        if (clientDetail==null){
            getDataForClient();
        }
        return clientDetail;
    }


    public static ConcurrentHashMap getTopic_Client(){
        if (topic_client==null){
            getDataForClient();
        }
        return topic_client;
    }

    public static ConcurrentHashMap<String,String> getUser_password(){
        if (user_password==null){
            getDataForClient();
        }
        return user_password;
    }
}
